const mongoose = require('mongoose')

/**
 * 项目指导模型
 * - projectId: 项目指导记录ID
 * - isSuccessCase: 是否成功案例（1:是, 0:否）
 * - completionDate: 项目完成时间
 * - files: 关联图片文件
 * - projectName: 项目名称
 * - industry: 所属行业
 * - description: 项目介绍
 * - partners: 合作企业列表
 */
const ProjectSchema = new mongoose.Schema(
  {
    projectId: {
      type: String,
      required: true,
      default: function () {
        return this._id.toString()
      }
    },
    isSuccessCase: {
      type: Number,
      required: true,
      default: 0
    },
    completionDate: {
      type: Date,
      required: true,
      get: v => v.toISOString().split('T')[0]
    },
    files: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'File',
      required: true
    },
    projectName: {
      type: String,
      required: true,
      maxlength: 100
    },
    industry: {
      type: String,
      required: true,
      maxlength: 50
    },
    description: {
      type: String,
      required: true,
      maxlength: 1000
    },
    partners: [
      {
        type: String,
        maxlength: 50
      }
    ],
    year: {
      type: Number,
      required: true,
      default: function () {
        return new Date(this.completionDate).getUTCFullYear()
      }
    },
    createdAt: {
      type: Date,
      default: Date.now
    }
  },
  { timestamps: true }
)

ProjectSchema.set('toJSON', { getters: true })
ProjectSchema.index({ year: 1, projectId: 1, isSuccessCase: 1 }) // 按年份

module.exports = mongoose.model('Project', ProjectSchema)
